package com.gec.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gec.system.mapper.SysMenuMapper;
import com.gec.system.mapper.SysRoleMenuMapper;
import com.gec.system.system.SysMenu;
import com.gec.system.system.SysRole;
import com.gec.system.service.SysRoleService;
import com.gec.system.mapper.SysRoleMapper;
import com.gec.system.system.SysRoleMenu;
import com.gec.system.system.SysUserRole;
import com.gec.system.utils.MenuHelper;
import com.gec.system.vo.AssignMenuVo;
import com.gec.system.vo.AssignRoleVo;
import com.gec.system.vo.SysRoleQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author 18236
 * @description 针对表【sys_role(角色)】的数据库操作Service实现
 * @createDate 2024-09-19 20:07:06
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole>
        implements SysRoleService {
    @Autowired
    SysRoleMenuMapper roleMenuMapper;
    @Autowired
    SysMenuMapper menuMapper;

    public IPage<SysRole> selectPage(IPage<SysRole> page, SysRoleQueryVo vo) {

        return this.baseMapper.selectPage(page,vo);
    }

    @Override
    public List<SysMenu> getMenuByRoleId(Long roleId) {
        QueryWrapper<SysRoleMenu> sysRoleMenuWrapper = new QueryWrapper<>();
        sysRoleMenuWrapper.eq("role_id",roleId);
        List<SysRoleMenu> sysRoleMenus = roleMenuMapper.selectList(sysRoleMenuWrapper);


        QueryWrapper<SysMenu> SysmenuWrapper = new QueryWrapper<>();
        SysmenuWrapper.eq("status", 1);
        List<SysMenu> menus = menuMapper.selectList(SysmenuWrapper);
        ArrayList<Long> roleMenuByIds = new ArrayList<>();
        for (SysRoleMenu sysRoleMenu : sysRoleMenus) {
            roleMenuByIds.add(sysRoleMenu.getMenuId());
        }
        for (SysMenu menu : menus) {
            if(roleMenuByIds.contains(menu.getId())){
                menu.setSelect(true);
            }
            else{
                menu.setSelect(false);
            }
        }
        List<SysMenu> MenuTree = MenuHelper.bulidTree(menus);
        return  MenuTree;
    }

    @Override
    public void doAssign(AssignMenuVo assignMenuVo ) {
        QueryWrapper<SysRoleMenu> wrapper = new QueryWrapper<>();
        roleMenuMapper.delete(wrapper.eq("role_id",assignMenuVo.getRoleId()));
        for (Long menuId : assignMenuVo.getRoleMenuIds()) {
            if (menuId!=null){
                SysRoleMenu sysRoleMenu = new SysRoleMenu();
                sysRoleMenu.setMenuId(menuId);
                sysRoleMenu.setRoleId(assignMenuVo.getRoleId());
                roleMenuMapper.insert(sysRoleMenu);
            }
        }

    }




}






